﻿using BenchmarkDotNet.Attributes;

namespace Esprima.Benchmark;

[MemoryDiagnoser]
public class FileParsingBenchmark
{
    private static readonly Dictionary<string, string> files = new()
    {
        { "underscore-1.5.2", null },
        { "backbone-1.1.0", null },
        { "mootools-1.4.5", null },
        { "jquery-1.9.1", null },
        { "yui-3.12.0", null },
        { "jquery.mobile-1.4.2", null },
        { "angular-1.2.5", null }
    };

    private JavaScriptParser _parser;

    [GlobalSetup]
    public void Setup()
    {
        foreach (var fileName in files.Keys.ToList())
        {
            files[fileName] = File.ReadAllText($"3rdparty/{fileName}.js");
        }

        _parser = new JavaScriptParser();
    }

    [ParamsSource(nameof(FileNames))]
    public string FileName { get; set; }

    public IEnumerable<string> FileNames()
    {
        foreach (var entry in files)
        {
            yield return entry.Key;
        }
    }

    [Benchmark]
    public void ParseProgram()
    {
        _parser.ParseScript(files[FileName]);
    }
}
